
#include "../MyWindow.h"
#include "../Camera.h"

#include "globals.h"

extern float rot_vec[3];
extern float move_vec[3];

class MainWindow : public MyBaseWindow {

	MyGLView main_viewport_;

	MyButton button_;
	cSlot< MainWindow, int > button_pressed_slot_;

	MyWinFrame toolbar_;

	//unique_ptr<MyTypeMesh> text_;


	MyDropMenu menu_;
	MyDropMenu menu2_;
	MyDropMenu menu3_;
	MyLinearLayout layout3_;

	MyEditBox edit_text_;

	bool cam_rot_enabled_;

public:
	//MyMainWindow(HWND window_handle, char flags ) : MyBaseWindow(window_handle, flags), cam_rot_enabled_(false){
	MainWindow( std::string title, int size_x, int size_y, char flags);

	void SetupMainViewPort ( Camera& cam ) {

		const int* v = main_viewport_.dims_px();
		
		cam.Perspective( 45.0f, (float)v[2]/v[3], 0.1f, 10000.0f );

		main_viewport_.SetActive();

	}

	virtual int OnClick( float x, float y) {
		MyBaseWindow::OnClick( x, y );

		//const float* v = main_viewport_->dims();
						
		//if( ( x >= v[0] ) &&
		//	( x <= v[0] + v[2] ) &&
		//	( y >= v[1] ) &&
		//	( y <= v[1] + v[3] ) ) 
		if( main_viewport_.Check( x, y ) )
		{
			//listen_mouse_move = true;
			cam_rot_enabled_ = true;

			extern bool view_update_needed;
			view_update_needed = true;
			//std::cout << "rotate!!! : " << x << " " << y << std::endl;
		} else
		{
			extern bool view_update_needed;
			view_update_needed = false;
		}

		button_.Check( x, y );

		layout3_.Check( x, y );

		edit_text_.Check( x, y );

		//menu_->Check( x, y );

		return 0;
	}

	virtual int OnMove( float dx, float dy ) {
		
		if( MyBaseWindow::OnMove( dx, dy ) == -1 )
		{
			return -1;
		}

		if( update_needed_ )
		{

		main_viewport_.Resize( -1.0f + 17.0f/window_size_[0],
								-1.0f + 0.2f + 17.0f/window_size_[1],
								2.0f - 32.0f/window_size_[0], 
								2.0f - 0.2f - 160.0f/window_size_[1],
								window_size_ );

		toolbar_.UpdateSize( -1.0f + 17.0f/window_size_[0],
							   1.0f - 128.0f/window_size_[1],
							   2.0f - 32.0f/window_size_[0], 
							   82.0f/window_size_[1],
							   window_size_ );

		//main_viewport_->Resize( window_size_ );

		button_.Resize( -1.0f + (17.0f + 17.0f)/window_size_[0],
						 1.0f - 112.0f/window_size_[1],
						 0.2f,
						 42.0f/window_size_[1], window_size_ );

		//button2_->Resize( -1.0f + 0.2f + 3.0f*17.0f/window_size_[0],
		//				  1.0 - 112.0f/window_size_[1],
		//				  0.2f,
		//				  42.0f/window_size_[1], window_size_ );

		layout3_.Resize( -0.8f + 2.0f*17.0f/window_size_[0], 1.0f - 112.0f/window_size_[1], 2.0f, 2.0f, window_size_ );

		edit_text_.Resize( 0.0f, 0.0f, 0.5f, 0.4f, window_size_ );
		//menu_->Resize(  0.0f, 0.8f, 2.0f, 2.0f, window_size_ );

		}

		update_needed_ = false;

		if( cam_rot_enabled_ )
		{
			
			rot_vec[0] = dy * 100.0f;
			rot_vec[1] = dx * 100.0f;

			g_render_state.current_cam->Rotate( rot_vec, 1.0f );

		}

		return 0;
	}

	virtual int OnRelease( float x, float y) {
		MyBaseWindow::OnRelease( x, y );

		rot_vec[0] = 0.0f;
		rot_vec[1] = 0.0f;

		button_.Check( x, y );

		//menu_->Check( x, y );

		cam_rot_enabled_ = false;

		return 0;
	}

	virtual int OnKeyPress( std::vector< unsigned char >& keys ) {

		edit_text_.Listen( keys );

		move_vec[0] = move_vec[1] = move_vec[2] = 0.0f;

		for(auto key : keys)
		{
			switch(key)
			{
				case 'w':
					move_vec[2] = -1.0f;
					break;
				case 's':
					move_vec[2] = 1.0f;
					break;
				case 'd':
					move_vec[0] = 1.0f;
					break;
				case 'a':
					move_vec[0] = -1.0f;
					break;
				case 16:
					move_vec[1] = 1.0f;
					break;
				case (char)SDLK_LCTRL:
					move_vec[1] = -1.0f;
					break;
			}
		}

		return 0;
	}

	void Draw() {
		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
		MyBaseWindow::Draw();

		toolbar_.Draw();

		button_.Draw();

		main_viewport_.Draw();

		layout3_.Draw();

	}

	int ButtonProc () {

		std::cout << "proc called" << std::endl;

		g_render_state.wireframe_mode = !g_render_state.wireframe_mode;

		extern bool view_update_needed;
		view_update_needed = true;

		return 0;
	}

	int Quit(){
		g_quit = true;
	}
};
